//JavaScript Document

//-------------------------------------------
//  Generate类负责生成Brick对象的随机化坐标
//-------------------------------------------

function BrickManager(logic)
{
    var _this = this;
    _this.bricks = undefined;
    _this.ready = false;
    _this.count = 0;

    _this.preBrickY = 0; //最上方的盘子的Y坐标
    _this.curBrickY = 0;
    _this.brickWidth = 0;
    _this.brickHeight =0;
    _this.logic = logic;    
    
    _this.minDistance = 0;
    _this.maxDistance = 0;
    _this.crackedPlateProbability = 0; //产生裂盘子的概率
    _this.movingPlateProbability = 0; //产生会移动的盘子的概率
	_this.sp = 1;//盘子的移动速度
       
    _this.imgSrc = [	
    	{
			"path" : "./images/plate.png",
			"img" : undefined,
		},
		{
			"path" : "./images/crackedPlate1.png",
			"img"  : undefined,
		},
		{
			"path" : "./images/crackedPlate2.png",
			"img"  : undefined,
		}
	]
	
	_this.imgLoadCallBack = function()
	{
		// 如果所有图片加载完成, 那么设置ready为真
		_this.count += 1;

		if(_this.count == _this.imgSrc.length)
		{
			_this.ready = true;
		}
	}
	
	
	_this.createBricks = function(bricksNum)
	{
    	//初始化第一块Brick
    	var firstBrick = new StaticBrick(new Vector2(190, _this.curBrickY), new Rectangle(_this.brickWidth, _this.brickHeight));
    	firstBrick.img = _this.imgSrc[0].img;
    	_this.bricks.push(firstBrick);	
    	
    	for( var i = 1; i < bricksNum; i++)
    	{
    		var brick = _this.addStaMovBrick();
    		_this.bricks.push(brick);	
    		
    		//以一定的概率产生裂盘子
    		if( (brick = insertBrick()) != null){
    			_this.bricks.push(brick);	
    		}
    	}
		
		return _this.bricks;
	}
	
	_this.addStaMovBrick = function()
	{
		//随机产生砖块的坐标
		var maxY = _this.curBrickY - _this.minDistance - _this.brickHeight;
		var minY = _this.curBrickY - _this.maxDistance - _this.brickHeight;	
    	var ranX = Math.floor(Math.random()*375+3);
    	var ranY = Math.floor(Math.random()*(maxY-minY)+minY);
    	
    	//以一定概率判断产生移动砖块还是静止砖块
		var ran = Math.floor(Math.random()*9+1)/10;		
    	if( 0 < ran && ran <= _this.movingPlateProbability) //产生移动的盘子
    	{
    		var brick = new MovingBrick(new Vector2(ranX,ranY), new Rectangle(_this.brickWidth, _this.brickHeight));  
    		brick.img = _this.imgSrc[0].img;			
    	}else //产生静止的盘子
    	{
    		var brick = new StaticBrick(new Vector2(ranX,ranY), new Rectangle(_this.brickWidth, _this.brickHeight));  
    		brick.img = _this.imgSrc[0].img;
    	}
    	
    	_this.preBrickY = _this.curBrickY;
    	_this.curBrickY = ranY;
		
		return brick;
	}
	
	var insertBrick = function()
	{
		if( _this.preBrickY - _this.curBrickY <= (_this.brickHeight*2))
			return null;
		//随机产生砖块的坐标
		var maxY = _this.preBrickY - _this.brickHeight;
		var minY = _this.curBrickY + _this.brickHeight;	
    	var ranX = Math.floor(Math.random()*375+3);
    	var ranY = Math.floor(Math.random()*(maxY-minY)+minY);
    	
		//以一定的概率产生裂盘子
		var ran = Math.floor(Math.random()*9+1)/10;		
    	if( 0 < ran && ran <= _this.crackedPlateProbability) 
    	{
    		var brick = new CrackedBrick(new Vector2(ranX,ranY), new Rectangle(_this.brickWidth, _this.brickHeight));  
    		brick.img.push(_this.imgSrc[1].img);
    		brick.img.push(_this.imgSrc[2].img);
    		
    		return brick;
    	}else{
    		return null;
    	}
	}
	
	_this.update = function(marks)
	{
		for(var i = 0; i < _this.bricks.length; i++)
		{
			_this.logic.physics.updateObject(_this.bricks[i].physicsNode, 1);
			
			_this.bricks[i].update();			
		} 
		for( var i = 0; i < _this.bricks.length; i++)
       	    {
       	    	if( _this.bricks[i].name == "movingBrick" )
       	    	{
       	    		if( _this.bricks[i].physicsNode.velocity.x < 0)
       	    			_this.bricks[i].physicsNode.velocity.x = -_this.sp;
       	    		else
       	    			_this.bricks[i].physicsNode.velocity.x = _this.sp;
       	    	}     	    	
       	    }
		if( 3000 < marks && marks <= 5000)
		{
			_this.minDistance = 30;
			_this.maxDistance = 100;	
			_this.crackedPlateProbability = 3/10;
       	    _this.movingPlateProbability = 3/10;
			_this.sp = 2;
		}else if( 5000 < marks && marks <= 8000)
		{
			_this.minDistance = 50;
			_this.maxDistance = 120;	
			_this.crackedPlateProbability = 2/10;
       	    _this.movingPlateProbability = 4/10;
			_this.sp = 3;
       	    
		}else if ( 8000 < marks && marks <= 11000)
		{
			_this.minDistance = 70;
			_this.maxDistance = 140;	
			_this.crackedPlateProbability = 2/10;
       	    _this.movingPlateProbability = 5/10;
			_this.sp = 4
			
		}else if( 11000 < marks && marks <= 14000)
		{
			_this.minDistance = 90;
			_this.maxDistance = 160;	
			_this.crackedPlateProbability = 1/10;
       	    _this.movingPlateProbability = 7/10;
			_this.sp = 5;
		}else if( marks > 14000)
		{
			_this.minDistance = 100;
			_this.maxDistance = 170;	
       	    _this.movingPlateProbability = 1;
			_this.sp = 6;
		}
	}
		
	//当背景移动时，砖块也要进行相应移动
	_this.changeBricksCoordY = function(incrY)
	{		
		_this.curBrickY += incrY;
		_this.perBrickY += incrY;
		var length = _this.bricks.length;
		for( var i = 0; i < length; i++ )
		{
			_this.bricks[i].physicsNode.position.y += incrY;
			_this.bricks[i].position.y += incrY;
			//当砖块Y坐标超过背景高度时，要重新生成砖块
			if( _this.bricks[i].position.y > _this.logic.sceneManager.mHeight)
			{
				//删除砖块
				_this.logic.sceneManager.deleteNodeWith(_this.bricks[i]);
				var name = _this.bricks[i].name;
				_this.bricks.splice(i,1);
				length--;  
				i--;
				//生成新的砖块
				if( name != "crackedBrick")
				{					
					var brick = _this.addStaMovBrick();
					_this.bricks.push(brick);	
					var node = _this.logic.sceneManager.createNode(brick, 4);           
               	    _this.logic.sceneManager.addNode(node); 	
               	    			
               	   //以一定的概率产生裂盘子
    				if( (brick = insertBrick()) != null)
    				{
    					_this.bricks.push(brick);	
    					var node = _this.logic.sceneManager.createNode(brick, 3);           
               	   	    _this.logic.sceneManager.addNode(node); 
               	   }
    			}
				
			}
		}
	}

	
	_this.touch = function(brick)
	{
		brick.touch();
	}
	
	var init = function ()
	{
		_this.bricks = new Array();
		_this.curBrickY = _this.logic.sceneManager.mHeight - 50;
		_this.brickWidth = 100;
		_this.brickHeight = 33;
		
		_this.minDistance = 15;
		_this.maxDistance = 75;	
		_this.crackedPlateProbability = 4/10;
        _this.movingPlateProbability = 2/10;
		
		for(var i = 0; i < _this.imgSrc.length; ++i)
	    {
			var img = new Image();
			img.src = _this.imgSrc[i].path;
			_this.imgSrc[i].img = img;
			// 设置图片的完成回调函数, 用于完成资源的载入调度
			img.onload = _this.imgLoadCallBack;
	    }
	}
	init();  
}
